#!/usr/bin/python
# -*- coding: utf-8 -*-

#This file is part of pyLot library.
#
# pyLot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyLot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pyLot.  If not, see <http://www.gnu.org/licenses/>.

__author__ = u'Pierre Puiseux, Guillaume Baty'
__copyright__ = u"Copyright 2011-2012 (C) andheo, Université de Pau et des Pays de l'Adour"
__credits__ = [u'Pierre Puiseux', u'Guillaume Baty']
__license__ = "GNU Lesser General Public License"

from Qt4 import QtGui

from _pylot.qtgui.interfaces.widgetinterface import WidgetInterface
from _pylot.qtgui.widgets.gui_containereditor import GuiContainerEditor
from _pylot.qtgui.widgets.gui_autopreferences import GuiAutoPrefs

class GuiAutoWidget(WidgetInterface):
  def __init__ (self, handler=None):
    WidgetInterface.__init__(self, handler)
    QtGui.QWidget.__init__(self)

  def autoCreateMenus(self, pylot_widget):
    u"""
    Generates menus by concatenating child menus
    """
    main_menu = QtGui.QMenu(self._plugin_path.name)
    for menu in pylot_widget.menus :
      main_menu.addMenu(menu)
    self.addMenu_(main_menu)

  def autoCreateToolBars(self, pylot_widget):
    u"""
    Generates toolbars by concatenating child toolbars
    """
    for tool_bar in pylot_widget.tool_bars :
      self.addToolBar_(tool_bar)

  def autoCreatePreferences(self, pylot_widget):
    u"""
    Generates preference widget by concatenating child preference widget
    """
    plugin_pref = pylot_widget.pref

    if not isinstance(self.pref, GuiAutoPrefs) :
      self.pref = GuiAutoPrefs(handler=self.handler)

    if plugin_pref :
      self.pref.addPrefWidget(plugin_pref)

  def autoCreatePreferenceWidget(self):
    if self.handler.hasUserOptions() :
      pref = GuiAutoPrefs(handler=self.handler)
      pref_widgets = []
      for user_options in self.handler._user_options :
        widget = GuiContainerEditor(user_options, handler=self.handler)
        setattr(pref, user_options.name, widget)
        pref.addPrefWidget(widget)
      self._pref = pref

  def _createPreferences(self):
    self.autoCreatePreferenceWidget()

  def addPlugin(self, name, handler=None, plugin_id=None, auto_fill_widget=True):
    gui = WidgetInterface.addPlugin(self, name, handler, plugin_id,
                                    auto_fill_widget)
    if auto_fill_widget is True :
      self.autoCreateToolBars(gui)
      self.autoCreateMenus(gui)
      self.autoCreatePreferences(gui)
    return gui

